const mongoose = require('mongoose');
var express = require('express')
var addressXian = express()

const {createResponse}= require("../../util/createResponse/index");
const {businessCodes}= require("../../code/businessCodes/index");
const {addressXianService } = require('../../service/impl/addressXianService/index');


addressXian.post('/post/addressXian/add', async (req,res) => {
  let result
  try {
    let news  = req.body.news
    result =  await addressXianService.getIns().create(news)
  } catch (error) {
    console.log(error);
    createResponse(res,500,businessCodes.b2)
    return
  }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  addressXian.post('/post/addressXian/delete', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      result =  await addressXianService.getIns().findByIdAndDelete(id)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  addressXian.post('/post/addressXian/update', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      let updates = req.body.updates
      result =  await addressXianService.getIns().findByIdAndUpdate(id,updates)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  addressXian.post('/post/addressXian/query', async (req,res) => {
    let result
    try {
      let fills = {
        "key1":{
          "path":"shijiID",
        },
        "key2":{
          "path":"shengjiID",
        }
      }
      result =  await addressXianService.getIns().find(null,null,fills)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })


  // 根据市ID,查询包含的区县
  addressXian.post('/post/shiji/queryXianByShiID',  async (req,res) => {
    // controller.quxianController.getSigleIns().findByObj(req,res)
  })
  
  module.exports= addressXian